package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.JtSupplier;
import com.ruoyi.business.domain.JtWarehouse;
import com.ruoyi.business.domain.SysUserSupplier;
import com.ruoyi.business.service.IJtSupplierService;
import com.ruoyi.business.service.IJtWarehouseService;
import com.ruoyi.business.service.ISysUserSupplierService;
import com.ruoyi.business.utils.SnGenerateUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtPurchaseOrder;
import com.ruoyi.business.service.IJtPurchaseOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/business/purchase")
public class JtPurchaseOrderController extends BaseController
{
    private String prefix = "business/purchase";

    @Autowired
    private IJtPurchaseOrderService jtPurchaseOrderService;
    @Autowired
    private IJtWarehouseService jtWarehouseService;
    @Autowired
    private IJtSupplierService jtSupplierService;
    @Autowired
    private ISysUserSupplierService sysUserSupplierService;


    @RequiresPermissions("business:purchase:view")
    @GetMapping()
    public String purchase()
    {
        return prefix + "/purchase";
    }

    /**
     * 查询采购单列表
     */
    @RequiresPermissions("business:purchase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtPurchaseOrder jtPurchaseOrder)
    {
        startPage();
        List<JtPurchaseOrder> list = jtPurchaseOrderService.selectJtPurchaseOrderList(jtPurchaseOrder);
        return getDataTable(list);
    }

    //===================供应商==================

    @RequiresPermissions("business:supplier:purchase:view")
    @GetMapping("/supplier")
    public String supplierPurchase()
    {
        return prefix + "/supplier/purchase";
    }

    /**
     * 查询采购单列表
     */
    @RequiresPermissions("business:supplier:purchase:list")
    @PostMapping("/supplier/list")
    @ResponseBody
    public TableDataInfo purchaseList(JtPurchaseOrder jtPurchaseOrder)
    {
        SysUserSupplier sysUserSupplier = sysUserSupplierService.selectSysUserSupplierByUserId(ShiroUtils.getUserId());
        if(sysUserSupplier == null || sysUserSupplier.getSupplierId() == null){
            return new TableDataInfo();
        }
        jtPurchaseOrder.setSupplierId(sysUserSupplier.getSupplierId());
        startPage();
        List<JtPurchaseOrder> list = jtPurchaseOrderService.selectJtPurchaseOrderList(jtPurchaseOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @RequiresPermissions("business:purchase:export")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtPurchaseOrder jtPurchaseOrder)
    {
        List<JtPurchaseOrder> list = jtPurchaseOrderService.selectJtPurchaseOrderList(jtPurchaseOrder);
        ExcelUtil<JtPurchaseOrder> util = new ExcelUtil<JtPurchaseOrder>(JtPurchaseOrder.class);
        return util.exportExcel(list, "采购单数据");
    }

    @RequiresPermissions("business:purchase:view")
    @GetMapping("/view/{purchaseId}")
    public String view(@PathVariable("purchaseId") Long purchaseId, ModelMap mmap)
    {
        JtPurchaseOrder jtPurchaseOrder = jtPurchaseOrderService.selectJtPurchaseOrderByPurchaseId(purchaseId);
        JtSupplier jtSupplier = jtSupplierService.selectJtSupplierBySupplierId(jtPurchaseOrder.getSupplierId());
        JtWarehouse jtWarehouse = jtWarehouseService.selectJtWarehouseByWarehouseId(jtPurchaseOrder.getWarehouseId());
        if(jtSupplier != null) {
            jtPurchaseOrder.setSupplierName(jtSupplier.getSupplierName());
        }
        if(jtWarehouse != null) {
            jtPurchaseOrder.setWarehouseName(jtWarehouse.getWarehouseName());
        }
        mmap.put("jtPurchaseOrder", jtPurchaseOrder);
        return prefix + "/view";
    }

    /**
     * 新增采购单
     */
    @RequiresPermissions("business:purchase:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("purchaseNo", SnGenerateUtil.purchaseOrderNumber());
        return prefix + "/add";
    }

    /**
     * 新增保存采购单
     */
    @RequiresPermissions("business:purchase:add")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtPurchaseOrder jtPurchaseOrder)
    {
        JtPurchaseOrder param = new JtPurchaseOrder();
        param.setOrderNo(jtPurchaseOrder.getOrderNo());
        List<JtPurchaseOrder> purchaseOrderList = jtPurchaseOrderService.selectJtPurchaseOrderList(param);
        if(purchaseOrderList.size() > 0){
            return AjaxResult.error(jtPurchaseOrder.getOrderNo() + "采购单已存在，请勿重创建！");
        }
        return toAjax(jtPurchaseOrderService.insertJtPurchaseOrder(jtPurchaseOrder));
    }

    /**
     * 修改采购单
     */
    @RequiresPermissions("business:purchase:edit")
    @GetMapping("/edit/{purchaseId}")
    public String edit(@PathVariable("purchaseId") Long purchaseId, ModelMap mmap)
    {
        JtPurchaseOrder jtPurchaseOrder = jtPurchaseOrderService.selectJtPurchaseOrderByPurchaseId(purchaseId);

        JtSupplier jtSupplier = jtSupplierService.selectJtSupplierBySupplierId(jtPurchaseOrder.getSupplierId());
        JtWarehouse jtWarehouse = jtWarehouseService.selectJtWarehouseByWarehouseId(jtPurchaseOrder.getWarehouseId());
        if(jtSupplier != null) {
            jtPurchaseOrder.setSupplierName(jtSupplier.getSupplierName());
        }
        if(jtWarehouse != null) {
            jtPurchaseOrder.setWarehouseName(jtWarehouse.getWarehouseName());
        }
        mmap.put("jtPurchaseOrder", jtPurchaseOrder);

        return prefix + "/edit";
    }

    /**
     * 修改保存采购单
     */
    @RequiresPermissions("business:purchase:edit")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtPurchaseOrder jtPurchaseOrder, String removeItemIds)
    {
        return toAjax(jtPurchaseOrderService.updateJtPurchaseOrder(jtPurchaseOrder, removeItemIds));
    }

    /**
     * 删除采购单
     */
    @RequiresPermissions("business:purchase:remove")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtPurchaseOrderService.deleteJtPurchaseOrderByPurchaseIds(ids));
    }

    /**
     * 审核 采购
     */
    @RequiresPermissions("business:purchase:audit")
    @Log(title = "采购单-审核", businessType = BusinessType.UPDATE)
    @PostMapping( "/audit")
    @ResponseBody
    public AjaxResult audit(String ids)
    {
        return toAjax(jtPurchaseOrderService.auditJtPurchaseOrderByPurchaseIds(ids));
    }

    /**
     * 反审核 采购单
     */
    @RequiresPermissions("business:purchase:audit")
    @Log(title = "采购单-反审核", businessType = BusinessType.UPDATE)
    @PostMapping( "/unAudit")
    @ResponseBody
    public AjaxResult unAudit(String ids)
    {
        return toAjax(jtPurchaseOrderService.unAuditJtPurchaseOrderByPurchaseIds(ids));
    }

    /**
     * 完成 采购单
     */
    @RequiresPermissions("business:purchase:audit")
    @Log(title = "采购单-完成", businessType = BusinessType.UPDATE)
    @PostMapping( "/finished")
    @ResponseBody
    public AjaxResult finished(String ids)
    {
        return toAjax(jtPurchaseOrderService.finishedJtPurchaseOrderByPurchaseIds(ids));
    }
}
